
// let hospitalData = require('hospitalData')
Page({
  data: {
    centerX: 0.0,
    centerY: 0.0,
    //可能我标识的地点和你所在区域比较远，缩放比例建议5;
    scale: 15,
    markers: [],
    latitude: "",
    longitude: "",
    hospitalData:[]
  },
  // controls: [{
  //   id: 1,
  //   iconPath: '/images/location-control.png',
  //   position: {
  //     left: 0,
  //     top: 10,
  //     width: 40,
  //     height: 40
  //   },
  //   clickable: true
  // }]
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')
  },
  onLoad: function (options) {
    console.log(options);
  
    this.setData({
      hospitalData:[
        {
          "id": 1,
          "path": "/icons/worker.png",
          "name": "接单方",
          "longitude": options.lng2,
          "latitude": options.lat2

        },
        {
          "id": 2,
          "path": "/icons/local.png",
          "name": "发单方",
          "longitude": options.lng1,
          "latitude": options.lat1

        },
        
      ],
      latitude: options.lat2,
      longitude: options.lng2,
     
    })
    console.log('地图定位！2')
    let that = this
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: (res) => {
        console.log("111111")
        let latitude = res.latitude;
        let longitude = res.longitude;
        let marker = this.createMarker(res);
        this.setData({
         
          centerX: longitude,
          centerY: latitude,
          markers: this.getHospitalMarkers()
        })
      },
      fail(res) {
        wx.getSetting({ //获取系统设置过的权限
          success: (res) => {
            console.info('getSetting suc', res);
            /*
             * res.authSetting = {
             *   "scope.userInfo": true,
             *   "scope.userLocation": true
             * }
             */

          },
          fail: (res) => {
            console.info('getSetting fail', res);

          }
        })
        console.log(res);
      }
    });
    this.controltap();
  },
  /**
   * 标示点移动触发
   */
  regionchange(e) {
    console.log(e.type)
  },
  /**
   * 点击标识点触发
   */
  markertap(e) {
    console.log(e)
  },
  /**
   * control控件点击时间
   */
  controltap() {
    // console.log(e);
    // console.log(e.controlId)
    this.moveToLocation()
  },
  /**
   * 获取医院标识
   */
  getHospitalMarkers() {
    let markers = [];
    console.log("获取地图中的位置")
    for (let item of this.data.hospitalData) {
      let marker = this.createMarker(item);
      console.log(item);
      markers.push(marker)
    }
    return markers;
  },
  /**
   * 移动到自己位置
   */
  moveToLocation: function () {
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
  },
  /**
   * 还有地图标识，可以在name上面动手
   */
  createMarker(point) {
    let latitude = point.latitude;
    let longitude = point.longitude;
    let marker = {
      iconPath: point.path,
      id: point.id || 0,
      name: point.name || '',
      latitude: latitude,
      longitude: longitude,
      width: 30,
      height: 45
    };
    return marker;
  }
})